
package com.zhangyunfang.android.demo.popupmenu;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;

public final class PopopMenu extends AbsPopupMenuPage {

    private static final int ID_POPUP_MENU_A = 0x1111;

    private static final int ID_POPUP_MENU_B = 0x1112;

    private static final int ID_POPUP_MENU_C = 0x1113;

    private static final int ID_POPUP_MENU_D = 0x1114;

    private static final int ID_POPUP_MENU_E = 0x1115;

    private static final int MAX_POPUP_MENU_ITEMS = 5;

    private static final ArrayList<MenuItem> sItems = new ArrayList<MenuItem>();
    static {
        sItems.add(new MenuItem(1, ID_POPUP_MENU_A, R.drawable.stat_notify_alarm, "Alarm"));
        sItems.add(new MenuItem(2, ID_POPUP_MENU_B, R.drawable.stat_notify_car_mode, "Car Mode"));
        sItems.add(new MenuItem(3, ID_POPUP_MENU_C, R.drawable.stat_notify_chat, "Chat"));
        sItems.add(new MenuItem(4, ID_POPUP_MENU_D, R.drawable.stat_notify_sim_toolkit, "Sim Toolkit"));
        sItems.add(new MenuItem(5, ID_POPUP_MENU_E, R.drawable.stat_notify_sync, "Sync"));
    }

    @Override
    protected void loadContentView(FrameLayout pageContent) {
    }

    @Override
    protected void loadContentData() {
    }

    @Override
    protected void createPopupMenuItem(ViewGroup parent, OnClickListener clickListener) {

        final LayoutInflater layoutInflater = getLayoutInflater();
        for (int i = 0; i < MAX_POPUP_MENU_ITEMS; i++) {
            if (i > 0 && i <= MAX_POPUP_MENU_ITEMS - 1) {
                final View line = layoutInflater.inflate(R.layout.item_popup_menu_item_line,
                        parent, false);
                parent.addView(line);
            }
            final TextView item = (TextView) layoutInflater.inflate(R.layout.item_popup_menu_item,
                    parent, false);
            item.setOnClickListener(clickListener);

            final MenuItem menuItem = sItems.get(i);
            item.setId(menuItem.id);
            item.setTag(menuItem.position);
            if (!TextUtils.isEmpty(menuItem.text)) {
                item.setText(menuItem.text);
            } else if (menuItem.textID != 0) {
                item.setText(menuItem.textID);
            }
            if (menuItem.iconID != 0) {
                item.setCompoundDrawablesWithIntrinsicBounds(menuItem.iconID, 0, 0, 0);
            }
            parent.addView(item);
        }
    }

    @Override
    protected void onPopupMenuItemClicked(View view, int position, int id) {
        Log.d("PopopMenu", "onPopupMenuItemClicked -- view = " + view + "  position = " + position + " id = " + id);
    }
}
